package com.example.listview;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {


    //标题
    private String[] titles = {"桌子", "苹果", "蛋糕", "线衣", "猕猴桃", "围巾"};
    //价格
    private String[] prices = {"1800元", "10元/kg", "300元", "350元", "10元/kg", "280元"};
    //图片
    private int[] icons = {R.drawable.table, R.drawable.apple, R.drawable.cake,
            R.drawable.wireclothes, R.drawable.kiwifruit, R.drawable.scarf};


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //1. 获取ListView对象
        ListView listView = findViewById(R.id.listview);
        MyAdapter myAdapter = new MyAdapter();
        listView.setAdapter(myAdapter);


        //单击事件
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(
                        MainActivity.this,
                        "标题：" + titles[position] + "，价格：" + prices[position],
                        Toast.LENGTH_SHORT
                ).show();
            }
        });

        //长按事件
        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                Toast
                        .makeText(MainActivity.this, "长按事件" + titles[position], Toast.LENGTH_SHORT)
                        .show();
                return true;
            }
        });
    }


    class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return titles.length;
        }

        @Override
        public Object getItem(int position) {
            return titles[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            VH holder;
            if (convertView == null) {
                //1. 加载布局
                convertView = View.inflate(MainActivity.this, R.layout.item_list, null);
                holder = new VH();
                //2. 获取控件
                holder.iv_icon = convertView.findViewById(R.id.iv);
                holder.tv_title = convertView.findViewById(R.id.title);
                holder.tv_price = convertView.findViewById(R.id.tv_price);
                convertView.setTag(holder);
            } else {
                holder = (VH) convertView.getTag();
            }

            //3. 设置数据
            holder.iv_icon.setImageResource(icons[position]);
            holder.tv_title.setText(titles[position]);
            holder.tv_price.setText(prices[position]);
            return convertView;
        }


        class VH {
            ImageView iv_icon;
            TextView tv_title;
            TextView tv_price;
        }
    }
}